#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/**
 * Definition for a Node.
 */
struct Node {
	int val;
	struct Node* left;
	struct Node* right;
	struct Node* next;
};
typedef struct Node node, *node_p;

struct Node* connect(struct Node* root);
void handle(node_p* last, node_p p, node_p* next_p);

int main()
{
	return 0;
}

struct Node* connect(struct Node* root)
{
	if (!root) return NULL;

	node_p p = root;
	while (p) {
		node_p last = NULL, next_p = NULL;
		for (node_p i = p; i; i = i->next) {
			if (i->left) 	handle(&last, i->left, &next_p);
			if (i->right) 	handle(&last, i->right, &next_p);
		}
		p = next_p;
	}
	return root;
}

void handle(node_p* last, node_p p, node_p* next_p)
{
	//这一行的上一结点
	if (*last) (*last)->next = p;
	//只会在每行开始被赋值一次, 标志下一行的开始
	if (!(*next_p)) *next_p = p;
	*last = p;
}
